package com.jame.basics.thread.juc;

/**
 * @author : Jame
 * @date : 2021-01-10 15:47
 **/
public class ReetrantLock2 {

    public static void main(String[] args) {
        Tickets1 tickets = new Tickets1();
        new Thread(() -> {
            for (int i = 0; i < 30; i++) {
                int c = tickets.sell();
                if (c == -1) {
                    System.out.println(Thread.currentThread().getName() + "没抢到");
                } else {
                    System.out.println(Thread.currentThread().getName() + "买到第" + c + "张票");
                }
            }
        }).start();
        new Thread(() -> {
            for (int i = 0; i < 30; i++) {
                int c = tickets.sell();
                if (c == -1) {
                    System.out.println(Thread.currentThread().getName() + "没抢到");
                } else {
                    System.out.println(Thread.currentThread().getName() + "买到第" + c + "张票");
                }
            }
        }).start();
        new Thread(() -> {
            for (int i = 0; i < 30; i++) {
                int c = tickets.sell();
                if (c == -1) {
                    System.out.println(Thread.currentThread().getName() + "没抢到");
                } else {
                    System.out.println(Thread.currentThread().getName() + "买到第" + c + "张票");
                }
            }
        }).start();


    }

}

class Tickets1 {
    int count = 30;

    public int sell() {
        if (count == 0)
            return -1;
        System.out.println("还剩" + (count--) + "张票");
        return count;

    }
}
